package com.lyrcsoft.controller;

import com.lyrcsoft.entities.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 通过实现{@link  Controller}接口，实现自定义的控制器。
 */
@Component("/updateUser.do")
// http://localhost:8080/tuling_web/app/updateUser.do?id=1&username=xxx&email=xxx@qq.com
public class MyController implements Controller {
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 1. 接收请求参数
		Long id = Long.parseLong(request.getParameter("id"));
		String username = request.getParameter("username");
		String email = request.getParameter("email");

		// 2. 封装数据对象
		User user = new User(id, username, email);

		// 3. 调用服务层
		boolean success = false;

		// 4. 创建返回视图
		ModelAndView mav = new ModelAndView();
		mav.setViewName("resultPage");  // JSP 视图名称

		if (success) {
			mav.addObject("message", "用户更新成功！");
		} else {
			mav.addObject("message", "用户更新失败：用户不存在");
		}
		return mav;
	}
}
